/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared.XML;

import avl.sv.shared.Annotations.AnnotationSet;
import avl.sv.shared.SlideReference;
import avl.sv.shared.SlideSet;
import avl.sv.shared.SlideSets;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author benbryan
 */
public class SlideSetsXML_Writer {
    
    public static String getXMLString(SlideSets slideSets) {
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
      
        try {
            DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            // root elements
            Document doc = docBuilder.newDocument();
            appendSlideSets(doc, slideSets);
            
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            
            StreamResult result = new StreamResult(bos);
            transformer.transform(source, result);

        } catch (TransformerException | ParserConfigurationException ex) {
            Logger.getLogger(SolutionXML_Writer.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return bos.toString();
    }

    protected static void appendSlideSets(Document doc, SlideSets slideSets) {
        Element slideSetsNode = doc.createElement("slideSets");
        doc.appendChild(slideSetsNode);
        for ( SlideSet slideSet : slideSets.getSlideSets()) {
            Element slideSetNode = doc.createElement("slideSet");
            slideSetNode.setAttribute("name", slideSet.getSlideSetName());
            slideSetsNode.appendChild(slideSetNode);
            appendSlideSet(doc, slideSetNode, slideSet);
        }
    }
    
    protected static void appendSlideSet(Document doc, Element slideSetNode, SlideSet slideSet) {       
        for (SlideReference slideRef:slideSet.getSlides()){
            appendSlideNode(doc, slideSetNode, slideRef);
        }
    }
    
    protected static void appendSlideNode(Document doc, Element slideSetNode, SlideReference slideRef) {       
        Element slideNode = doc.createElement("slide");
        slideNode.setAttribute("slideName", slideRef.slideName);
        slideNode.setAttribute("slideSetName", slideRef.slideSetName);
        slideNode.setAttribute("hash", DatatypeConverter.printBase64Binary(slideRef.hash));
        slideSetNode.appendChild(slideNode);
    }

}
